// pages/seach/seach.js
var app = getApp();
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    hotSeachData:'',
    historyData:'',
    seachData: [],
    seachShow:false,
    seachValue:'',
    typeShow:false,
    typeObj: { '1': '顺风车', '2': '活动', '3': '行业资源', '4': '品牌商家' },
    typeId:'1',
    page: 1,
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData: false,
    inputFocus:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    this.getHotSearch();
    this.getHisSearch();
    
  },
  enterPerson: function (e) {
    wx.navigateTo({
      url: '/pages/personalProfile/personalProfile?id=' + e.target.id
    })
  },
  // 查询热门搜索
  getHotSearch: function(){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'hotSearch/all',
      data: {},
      // method: 'POST',
      // header: {
      //   'content-type': 'application/x-www-form-urlencoded' // 默认值
      // },
      success: function (res) {
        _this.setData({
          hotSeachData: res.data.data
        })
      }
    })
  },
  // 查询历史搜索
  getHisSearch: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'searchInfo/page',
      data: {
        userId: this.data.userInfo.id,
        currentPage:1,
        pageNumber:'10'
      },
      success: function (res) {
        _this.setData({
          historyData: res.data.data.pp
        })
      }
    })
  },
  
  clearHistory:function(){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'searchInfo/delAll',
      data: {
        userId: this.data.userInfo.id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        _this.setData({
          historyData: []
        })
      }
    })
    
  },
  cancelSeach:function(){
    this.setData({
      seachValue: '',
      seachShow:false
    })
  },
  // 显示隐藏类型下拉选项
  typeTap:function(e){
    if (e.currentTarget.dataset.panel){
      this.setData({
        typeShow: false
      })
    }else{
      this.setData({
        typeShow: !this.data.typeShow
      })
    }
  },
  // 选择类型事件
  selType:function(e){
    this.setData({
      typeId: e.target.id,
      typeShow:false,
      seachShow:false,
      seachData:[],
      seachValue:''
    })
  },
  // 搜索
  changeInput:function(e){
        this.setData({
          seachValue: e.detail.value
        })
        if (e.detail.value != '') {
          this.setData({
            seachShow: true
          })
          if (this.data.typeId == '1') {
            this.getSeachDataSfc(true);
          } else if (this.data.typeId == '2') {
            this.getSeachDataHd(true);
          } else if (this.data.typeId == '3') {

          } else {
            this.getSeachDataSj();
          }
        } else {
          this.setData({
            seachShow: false
          })
        }
         
  },
  getSeachDataSfc(flag) {//查询顺风车数据
    var begintime_end = '';
    if (this.data.date != '') {
      begintime_end = util.getNow();
    }
    var _this = this;
    var param = {
      currentPage: this.data.page,
      pageNumber: '10',
      orderStatus: 1,
      place: this.data.seachValue
    };
    wx.request({
      url: app.globalData.httpApiCar + 'freeRideInfo/place', //仅为示例，并非真实的接口地址
      data: param,
      success: function (res) {
        if (res.data.data.constructor == Array) {
          if (res.data.data.length < 10) {
            _this.setData({
              hasData: false
            })
          } else {
            _this.setData({
              hasData: true
            })
          }
          res.data.data.forEach(function (ele) {
            ele.status = 0;//拼车中
            if (ele.createTime) {
              ele.time = util.getDays(ele.createTime)
            }
            if (ele.labelArray != undefined && ele.labelArray != '') {
              ele.mark = ele.labelArray.split('/');
            } else {
              ele.mark = [];
            }
          })
          if (flag) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            _this.setData({
              seachData: []
            });
          } else {
            _this.setData({
              loadMore: false
            });
          }
          _this.data.seachData = _this.data.seachData.concat(res.data.data);
          _this.setData({
            seachData: _this.data.seachData,
            hasData: _this.data.hasData
          })
          _this.addHistory(_this.data.seachValue, 1)
        }
      }
    })
  },
  showDetail: function (e) {
    // wx.setStorage({
    //   key: 'carDetail',
    //   data: e.currentTarget.dataset.item
    // })
    wx.navigateTo({
      url: '/pages/carDetail/carDetail?id=' + e.currentTarget.dataset.item.id
    })
  },
  getSeachDataSj:function(){//查询品牌商家数据
    var _this=this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopInfo/all',
      data: {
        shopName: this.data.seachValue
      },
      success: function (res) {
        if (res.data.success) {
          if (res.data.data.length > 0) {
            _this.setData({
              hasDataSj: true
            })
          }
          _this.setData({
            seachData: res.data.data
          })
          _this.addHistory(_this.data.seachValue, 2)
        }
      }
    })
  },
  getSeachDataHd:function(){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyActivity/getAllActivityDetailsByPage',
      data: {
        userId: this.data.userInfo.id,
        name: this.data.seachValue
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          if (typeof res.data.data.activities!='string') {
            res.data.data.activities.forEach(function (ele) {
              if (ele.releaseTime) {
                ele.time = util.formatTime(ele.releaseTime);
                // ele.time = util.getDays(new Date(ele.releaseTime.substr(0, 19).replace(/-/g, "/")).getTime());
              }
            })
            _this.data.seachData = res.data.data.activities;
          }else{
            _this.data.seachData = [];
          }
        }
        // if (flag) {
        //   wx.hideNavigationBarLoading() //完成停止加载
        //   wx.stopPullDownRefresh() //停止下拉刷新
        // }
        if (_this.data.seachData.length>0){
          _this.setData({
            hasDataHd: true
          })
        }
        _this.setData({
          seachData: _this.data.seachData
        })
        _this.addHistory(_this.data.seachValue,3)
      }
    })
  },
  openIndex:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/shopIndex/shopIndex?id=" + id
    })
  },
  addHistory: function (value,type) {
    wx.request({
      url: app.globalData.httpApiCar + 'searchInfo/save',
      data: {
        userId: this.data.userInfo.id,
        serachName: value,
        type: type,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
      }
    })
  },
  hotSeachTap:function(e){//热门搜索点击事件
    // e.currentTarget.dataset.item
    wx.navigateTo({
      url: "/pages/shopIndex/shopIndex?id=" + e.currentTarget.dataset.item.shopId
    })
  },
  historySeachTap:function(e){
    this.setData({
      inputFocus: true
    })
    this.changeInput({ detail: { value: e.currentTarget.dataset.item.serachName } });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      page: 1
    })
    if (this.data.typeId == '1') {
      this.getSeachDataSfc(true);
    } else if (this.data.typeId == '2') {
      this.getSeachDataHd();
    } else if (this.data.typeId == '3') {

    } else {
      this.getSeachDataSj();
    } 
  },
  scrolltoupper: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      page: 1
    })
    if (this.data.typeId == '1') {
      this.getSeachDataSfc(true);
    } else if (this.data.typeId == '2') {

    } else if (this.data.typeId == '3') {

    } else {
      this.getSeachDataSj();
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (this.data.typeId == '1') {
      if (that.data.hasData) {
        this.setData({
          loadMoreTxt: '正在加载数据。。。',
          loadMore: true,
          page: this.data.page + 1
        });
        this.getSeachDataSfc();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})